package org.tnc.budget.webapp.util;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import sun.misc.BASE64Decoder;



public class DESHelper {

	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

//		String str = "BBK3cIQ0pyZRUWSjLCWU624OHswQ+eXASgxx+jmgn32T+7He7+Ko/xV28lT+wjUikxW6ALdsRqULQiuwPQlUxZ1YJ7OrkOolsDBk4v3yWzbOc66xF7iG+Fq5WnpuHPrD0I3M8rs8mojNELNHqKEM09t8AWOuxPyUvFZCTmX4Jeubb/VKbT6K0mWXF5F8blpf4b2P5tiGGyY2ch6fEYcwo3xAQqRXWS7qD3N4/KMfLvPm8are8O4akiUpIluE4dS4i6AWDLOAMGuMd1EOzgGye+hY2wTkRgR1PUlQ8yzCR0ShTsRtNdVHnIwU8HKHAcF/XuSEuJVXPGBF3p9LK/deyVx3l/ZsJDZlN7dX/ZlAKtnH6/ye+OHLWbf7SX46K56HwUYinPBf1eZiqFdWxsPUlwamRa3v8OWU7dSpIhvSn/EeTH9Y3yqvn+MazLxwGbNX";
		String str = "mxsCmJQ/axE=";
		String pass = "c3949ba5";
		;
		String s = desDecrypt(str, pass);
		System.out.println(s);

	}

	public static String desDecrypt(String str, String pass) throws AuthCryptException {
		//str = str.replaceAll(" ", "+");
		
		/*BASE64Decoder decoder = new BASE64Decoder();
		byte[] bytesrc;

		try {
			bytesrc = decoder.decodeBuffer(str);
			
			Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			DESKeySpec desKeySpec = new DESKeySpec(pass.getBytes("UTF-8"));
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
			IvParameterSpec iv = new IvParameterSpec(pass.getBytes("UTF-8"));

			cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
			byte[] retByte = cipher.doFinal(bytesrc);
			String res = new String(retByte, "UTF-8");
			return res;
		} catch (IOException e) {
			throw new AuthCryptException(e);
		} catch (NoSuchAlgorithmException e) {
			throw new AuthCryptException(e);
		} catch (NoSuchPaddingException e) {
			throw new AuthCryptException(e);
		} catch (Exception e) {
			throw new AuthCryptException(e);
		}*/
		return str;
		
	}

}
